package com.wangjie.controller;

import com.wangjie.domain.PageResult;
import com.wangjie.domain.Result;
import com.wangjie.domain.dto.LoginUser;
import com.wangjie.domain.dto.RegisterUser;
import com.wangjie.domain.dto.UserQueryDto;
import com.wangjie.domain.vo.UserVo;
import com.wangjie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/list/{pageNum}/{pageSize}")
    public Result pageList(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize, UserQueryDto dto) {
        PageResult<UserVo> result = userService.pageUserList(pageNum, pageSize, dto);
        return Result.ok(result);
    }

    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable("id") Long id) {
        UserVo vo = userService.getUserById(id);
        return Result.ok(vo);
    }

    @PostMapping("/register")
    public Result userRegister(@RequestBody RegisterUser user) {
        userService.userRegister(user);
        return Result.ok();
    }

    @PostMapping("/login")
    public Result userLogin(@RequestBody LoginUser loginUser) {
        UserVo vo = userService.userLogin(loginUser.getUserName(), loginUser.getPassword());
        return Result.ok(vo);
    }

    @PostMapping("/logout")
    public Result userLogout() {
        boolean res = userService.userLogout();
        return Result.ok(res);
    }

    @GetMapping("/current")
    public Result getCurrentUser() {
        UserVo vo = userService.getCurrentUser();
        return Result.ok(vo);
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody List<Long> ids) {
        userService.deleteUserByIds(ids);
        return Result.ok();
    }
}
